package com.jcw.interceptor;

import cn.hutool.json.JSONUtil;
import com.jcw.common.R;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //判断session里有没有用户的id，如果有，就放行
        if (session.getAttribute("user") != null) {
            return true;
        }
        //判断session里有没有员工的id，如果有，就放行
        if (session.getAttribute("employee") != null) {
            return true;
        }

        response.getWriter().write(JSONUtil.toJsonStr(R.error("NOTLOGIN")));
        return false;
    }
}
